
    ##########################################################################################
    ### R code for Figure 7 of Supplemental Material
    ##########################################################################################
    
    # Note: This code requires "wnominate" and "pscl" packages in R and a dataset called "hou103kh.ord" (downloaded from Voteview.com)

    library(wnominate)
    library(pscl)

    Legislators <- read.fwf("hou103kh.ord", header=F, widths=c(3,5,2,2,7,4,1,1,11))
    All <- read.fwf("hou103kh.ord", header=F, widths=rep(1,1130))

    Votes <- All[,37:ncol(All)] 
    Info <- Legislators[,c(2,3,4,6)]
    row.names(Info) <- Legislators[,9]
    colnames(Info) <- c("ICPSR_ID", "ICPSR_state", "CD", "party")   

    Dat <- Legislators 
    Dat$legis.data <- Info
    Dat$votes <- Votes 
    Data1 <- rollcall(data=Dat$votes, legis.data=Dat$legis.data,
                     yea=c(1,2,3),
                     nay=c(4,5,6),
                     missing=c(7,8,9),
                     notInLegis=0)
    # Now Run WNOMINATE
    result <- wnominate(Data1, polarity = c(9, 13))   

    ### Now, plot cutline angles for foreign policy votes in 103rd House
    Votes <- read.csv("FP_All_H103.csv")
    original.label <- names(Votes)[6:ncol(Votes)]
    
    ### Decode the lable into vote list
    vote_list <- rep(NA, length(original.label))
    for(j in 1:length(original.label)){
    vote_list[j] <- as.numeric(strsplit(original.label, split="_v")[[j]][2]) 
    } 

    ## Modify "plot.angles" code to select foreign policy votes only
    plot.angles.select <- function (x, select, main.title = "Cutting Line Angles", x.title = "Angle in Degrees", 
                        y.title = "Count", dims = c(1, 2), ...) 
                    {
                        if (!class(x) == "nomObject") 
                            stop("Input is not of class 'nomObject'.")
                        if (x$dimensions == 1) 
                            stop("All angles in 1D NOMINATE are 90 degrees.")
                        if (length(dims) != 2) 
                            stop("'dims' must be an integer vector of length 2.")
                        weight <- x$weight[dims[2]]/x$weight[dims[1]]
                        contrained <- ((abs(x$rollcalls[, paste("spread", dims[1], 
                            "D", sep = "")]) > 0 | abs(x$rollcalls[, paste("spread", 
                            dims[2], "D", sep = "")]) > 0) & (x$rollcalls[, paste("midpoint", 
                            dims[1], "D", sep = "")]^2 + x$rollcalls[, paste("midpoint", 
                            dims[2], "D", sep = "")]^2) < 0.95)
                        cutvector1 <- na.omit(x$rollcalls[contrained, paste("spread", 
                            dims[2], "D", sep = "")] * weight/sqrt(x$rollcalls[contrained, 
                            paste("spread", dims[1], "D", sep = "")]^2 + weight^2 * 
                            x$rollcalls[contrained, paste("spread", dims[2], "D", 
                                sep = "")]^2))
                        cutvector2 <- -1 * na.omit(x$rollcalls[contrained, paste("spread", 
                            dims[1], "D", sep = "")]/sqrt(x$rollcalls[contrained, 
                            paste("spread", dims[1], "D", sep = "")]^2 + weight^2 * 
                            x$rollcalls[contrained, paste("spread", dims[2], "D", 
                                sep = "")]^2))
                        cutvector1[cutvector2 < 0] <- -cutvector1[cutvector2 < 0]
                        cutvector2[cutvector2 < 0] <- -cutvector2[cutvector2 < 0]
                        angles <- atan2(cutvector2, cutvector1) * 180/pi 
                        select.angles <- angles[select]
                        suppressWarnings(hist(select.angles, breaks = seq(0, 180, 10), main = main.title, 
                            xlab = x.title, ylab = y.title, cex.main = 1.2, cex.lab = 1.2, 
                            font.main = 2, at = seq(0, 180, 10), ...))
                    }
                   
    plot.angles.select(x=result, select=vote_list)                    

    postscript("Supplemental_Figure7.eps", height=10, width=5, horizontal=F)
    par(mfrow=c(2, 1))
    plot.coords(result, cutline = vote_list)   
    plot.angles.select(x=result, select=vote_list)  
    dev.off()
    

